﻿/*
 * FancyScrollView (https://github.com/setchi/FancyScrollView)
 * Copyright (c) 2020 setchi
 * Licensed under MIT (https://github.com/setchi/FancyScrollView/blob/master/LICENSE)
 */

using System.Collections.Generic;
using QFramework;
using UnityEngine;
using UnityEngine.SceneManagement;
using Cysharp.Threading.Tasks;
using DG.Tweening;

class UIChooseStage : MonoBehaviour,IController
{
    public List<StageData> itemData;
    public static UIChooseStage Instance;
    public CanvasGroup canvasGroup;

    [SerializeField] ScrollView scrollView = default;

    public IArchitecture GetArchitecture() =>MainArchitecture.Interface;

    private void Awake()
    {
        Instance = this;
        itemData = this.GetSystem<ProgressSystem>().StageProgress;
    }
    void Start()
    {
        
        var needChange2Win = this.GetModel<StageModel>().needChange2Win;
        var stageData = this.GetModel<StageModel>().m_StageData;
        if(needChange2Win == true && stageData != null)
        {
            stageData.NeedChange2Win = true;
        }
        scrollView.UpdateData(itemData);
        
        if(stageData != null)
        {
            int index = 0;
            for(index = 0; index < itemData.Count; index++)
            {
                if(itemData[index] == stageData) break;
            }
            scrollView.scroller.JumpTo(index);
            
        }
        scrollView.UpdateData(itemData);
    }

    public async void EnterGameAsync(StageData data)
    {
        Debug.Log("进入游戏");
        this.GetModel<StageModel>().m_StageData = data;
        await DOTween.To(()=> canvasGroup.alpha,x => canvasGroup.alpha = x,0,1);
        await SceneManager.LoadSceneAsync("GameScene");
    }
}
